package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        claims.put("status",employee.getStatus());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }
/**
 * 登录
 *
 * @param employeeLoginDTO
 * @return
 */
//@PostMapping("/login")
//public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
//    log.info("员工登录");
//    Employee employee=employeeService.login(employeeLoginDTO);
//    //登录成功后生成jwt令牌
//    Map<String,Object>claims=new HashMap<>();
//    claims.put(JwtClaimsConstant.EMP_ID,employee.getId());
//
//    String token= Jwts.builder()
//            .signWith(SignatureAlgorithm.HS256, "张莉莉")
//            .setClaims(claims)
//            .setExpiration(new Date(System.currentTimeMillis()+3600*1000))
//            .compact();
//    EmployeeLoginVO employeeLoginVO=EmployeeLoginVO.builder()
//            .id(employee.getId())
//            .userName(employee.getUsername())
//            .name(employee.getName())
//            .token(token)
//            .build();
//    return Result.success(employeeLoginVO);
//
//}


    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     * @param employeeDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增员工")
    public Result save(@RequestBody EmployeeDTO employeeDTO)
    {
        log.info("新增员工:{}",employeeDTO);
        employeeService.save(employeeDTO);
         return Result.success();
    }

    /**
     * 员工分页查询
     * @param employeePageQuryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQuryDTO)
    {
        log.info("员工分页查询：{}",employeePageQuryDTO);
        PageResult pageResult=employeeService.pageQury(employeePageQuryDTO);
        return Result.success(pageResult);
    }

    /**
     * 启用禁用员工
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用")
    public Result startOrStop(@PathVariable Integer status,Long id)
    {
        log.info("启用禁用员工:{},{}",status,id);
        employeeService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工")
    public Result<Employee> getById(@PathVariable Long id)
    {
        log.info("根据id查询员工:{}",id);
        Employee employee=employeeService.getById(id);
        return Result.success(employee);

    }

    /**
     * 更新员工
     * @param employeeDTO
     * @return
     */
    @PutMapping
    @ApiOperation("编辑员工")
    public Result update(@RequestBody EmployeeDTO employeeDTO)
    {
        log.info("更新员工:{}",employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }


}


